<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * This is a sample module for PyroCMS
 *
 * @author 		Jerel Unruh - PyroCMS Dev Team
 * @website		http://unruhdesigns.com
 * @package 	PyroCMS
 * @subpackage 	Sample Module
 */
class Withdrawals extends Public_Controller
{
	protected $_userData = null;
	public function __construct()
	{
		parent::__construct();
		$this->_userData = $this->session->all_userData();
		if (!isset($this->_userData['logged_in']) || !$this->_userData['logged_in']){
			redirect('./');
		}
		// Load all the required classes
		$this->load->library('form_validation');
		$this->load->model('uploking_withdrawals_m');
		$this->load->model('uploking_users_m');
		$this->lang->load('uploking');
		$this->load->helper('uploking');
		$this->config->load('uploking');
		/*
		$this->template->append_metadata(js('jquery.form.js', 'voucher'))
						->append_metadata(js('stoepje.voucher.frontend.js', 'voucher'))
						->append_metadata(css('stoepje.voucher.frontend.css', 'voucher'));
						*/
	}

	/**
	 * List all items
	 */
	public function index()
	{
		//set the base/default where clause
		$base_where = array('user_id' => $this->_userData['account']['user_id']);

		//add post values to base_where if f_module is posted
		$base_where = $this->input->post('f_prize') ? $base_where + array('f_prize' => $this->input->post('f_prize')) : $base_where;
		$base_where = $this->input->post('f_status') != '' ? $base_where + array('f_status' => $this->input->post('f_status')) : $base_where;
		$base_where = $this->input->post('f_keywords') ? $base_where + array('keywords' => $this->input->post('f_keywords')) : $base_where;

		// Create pagination links
		$total_rows = $this->uploking_withdrawals_m->count_by($base_where);
		$pagination = create_pagination('admin/news/index', $total_rows);

		// Using this data, get the relevant results
		$withdrawals = $this->uploking_withdrawals_m->limit($pagination['limit'])->get_many_by($base_where);

		//do we need to unset the layout because the request is ajax?
		$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
		
		$this->template
			->title($this->module_details['name'])
			->append_metadata(js('admin/filter.js'))
			->set('pagination', $pagination)
			->set('withdrawals', $withdrawals);

		$this->input->is_ajax_request() ? $this->template->build('index', $this->data) : $this->template->build('index', $this->data);
	}
	
	public function request()
	{
		if($this->input->post('request')){
			$result = array();
			$user = $this->uploking_users_m->get($this->_userData['account']['user_id']);
			$money = $this->input->post('money');
			$paypalEmail = $this->input->post('paypal_email');
			$pinNumber = $this->input->post('pin_number');
			$balancedMoney = getEarnedMoney($user->id) - getAllRequestWithdrawMoney($user->id);
			$errorMessage = '';
			
			if(is_numeric($money)){
				$money = $money + 0;
			}
			$this->load->helper('email');
			if(!is_numeric($money)){
				$errorMessage = lang('uploking_withdrawal_invalid_money_number');
			}else if(!is_int($money)){
				$errorMessage = lang('uploking_withdrawal_invalid_money_number');
			}else if($pinNumber != $user->pin){
				$errorMessage = lang('uploking_withdrawal_invalid_pin_number');
			}else if($paypalEmail == ""){
				$errorMessage = lang('uploking_withdrawal_invalid_email_address');
			}else if(!valid_email($paypalEmail)){
				$errorMessage = lang('uploking_withdrawal_invalid_email_address');
			}else if($money > $balancedMoney){
				$errorMessage = sprintf(lang('uploking_withdrawal_invalid_money_amount'), $balancedMoney);
			}
			if(!$errorMessage){
				$this->uploking_withdrawals_m->insert(array(
					'user_id'		=> $user->id,
					'email_paypal'	=> $paypalEmail,
					'pin'			=> $pinNumber,
					'money'			=> $money,
					'status'		=> 0,
					'create_time'	=> time()
				));
				
				$result['result'] = true;
				$result['message'] = lang('uploking_withdrawal_request_success_label');
			}else{
				$result['result'] = false;
				$result['message'] = $errorMessage;
			}
			echo json_encode($result);
		}else{
			$this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
			$this->template
				//->set('prize', $prize)
				->build('request', $this->data);
		}
	}
}
